package neu.soft.service.impl;

import neu.soft.entity.GoodInfo;
import neu.soft.entity.OperationPurchaseOrder;
import neu.soft.mapper.EIRelationMapper;
import neu.soft.mapper.EntryOrderMapper;
import neu.soft.message.MessageSource;
import neu.soft.service.EntryOrderService;
import neu.soft.vo.MsgBean;
import neu.soft.vo.PageBean;
import neu.soft.vo.ResultBean;
import org.apache.rocketmq.common.message.MessageConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;
import org.springframework.util.MimeTypeUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class EntryOrderServiceImpl implements EntryOrderService {
    @Autowired
    private EntryOrderMapper entryOrderMapper;

    @Autowired
    private EIRelationMapper eiRelationMapper;

    @Autowired
    private MessageSource source;

    public <T> boolean confirmPurchase(T msg, Integer delay){
        Message<T> message = MessageBuilder.withPayload(msg)
                .setHeader(MessageConst.PROPERTY_DELAY_TIME_LEVEL, delay)
                .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON)
                .build();
        System.out.println(message);
        return source.confirmPurchase().send(message);
    }

    public <T> boolean confirmDistribute(T msg, Integer delay){
        Message<T> message = MessageBuilder.withPayload(msg)
                .setHeader(MessageConst.PROPERTY_DELAY_TIME_LEVEL, delay)
                .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON)
                .build();
        System.out.println(message);
        return source.confirmDistribute().send(message);
    }

    /**
     * 新增一条入库单
     * @param map
     * @return
     */
    @Override
    public ResultBean<Integer> addEntryOrder(Map<String,Object> map){
        entryOrderMapper.addEntryOrder(map);
        return ResultBean.success();
    }

    /**
     * 查询某一时间段内所有入库记录
     * @param map
     * @return
     */
    @Override
    public ResultBean<PageBean<Map<String,Object>>> queryEntryOrder(Map<String,Object> map) throws ParseException {
        ResultBean<PageBean<List<Map<String,Object>>>> resultBean = new ResultBean<>();
        PageBean<Map<String,Object>> pageBean = new PageBean<>();
        int pageNow = (int) map.get("pageNow");
        int pageSize = (int) map.get("pageSize");
        List<Map<String,Object>> eList = entryOrderMapper.queryEntryOrder(map);
        for(Map<String,Object> eMap:eList){
            Date date = (Date) eMap.get("inTime");
            if(date!=null){
                SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd");
                String s2 = sdf2.format(date);
                eMap.put("inTime",s2);
            }
        }
        Long entryRows = entryOrderMapper.getEntryRows(map);
        if(eList == null){
            return ResultBean.error(404,"根据所给条件没有入库记录");
        }
        else {
            pageBean.setRowData(eList);
            pageBean.setTotalRows(entryRows);
            pageBean.setTotalPages((int) (entryRows % pageSize == 0 ? (entryRows/pageSize) : (entryRows/pageSize + 1)));
            return ResultBean.success(pageBean);
        }
    }

    /**
     * 查询所有的入库单和其中包含的商品
     * @author zhh
     * @return
     */
    @Override
    public ResultBean<PageBean<Map<String,Object>>> showAllEntryOrder(Map<String,Object> map) throws Exception {
        ResultBean<PageBean<List<Map<String,Object>>>> resultBean = new ResultBean<>();
        PageBean<Map<String,Object>> pageBean = new PageBean<>();
        int pageNow = (int) map.get("pageNow");
        int pageSize = (int) map.get("pageSize");
        List<Map<String,Object>> eList = entryOrderMapper.showAllEntryOrder(map);
        for(Map<String,Object> eMap:eList){
            Date date = (Date) eMap.get("inTime");
            if(date!=null){
                SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd");
                String s2 = sdf2.format(date);
                eMap.put("inTime",s2);
            }
        }
        Long entryRows = entryOrderMapper.getEntryRows(map);
        if(eList == null){
            return ResultBean.error(404,"入库单列表为空");
        }
        else {
            pageBean.setRowData(eList);
            pageBean.setTotalRows(entryRows);
            pageBean.setTotalPages((int) (entryRows % pageSize == 0 ? (entryRows/pageSize) : (entryRows/pageSize + 1)));
            return ResultBean.success(pageBean);
        }
    }

    /**
     * 根据所给的入库单号删除入库单,同时删除对应的货物记录
     * @author zhh
     * @param map
     */
    @Override
    public ResultBean<Integer> deleteEntryOrder(Map<String,Object> map){
        entryOrderMapper.deleteEntryOrder(map);
        return ResultBean.success();
    }

    @Override
    public int batchInsertGoodInfo(List<GoodInfo> goodInfoList) {
        entryOrderMapper.batchInsertGoodInfo(goodInfoList);
        return 0;
    }

    @Override
    public void addPurchaseOrder(OperationPurchaseOrder purchaseOrder){
        entryOrderMapper.addPurchaseOrder(purchaseOrder);
    }

    public int existEntryOrder(String entryOrderId){
        return entryOrderMapper.existEntryOrder(entryOrderId);
    }

    public List<Map<String,Object>> queryGoodInfo(int goodOrderId){
        return entryOrderMapper.queryGoodInfo(goodOrderId);
    }

    public String queryOrderIdByPurchase(int purchaseOrderId){
        return entryOrderMapper.queryOrderIdByPurchase(purchaseOrderId);
    }
}
